package com.xieyong.mywebsite.mapper;

import com.xieyong.mywebsite.model.entity.User;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface UserMapper {

    @Insert("INSERT INTO base_user (username,password,role,create_time,update_time) " +
            " VALUES (#{username},#{password},'USER',now(),now());")
    void register(String username, String password);

    @Insert("INSERT INTO base_user(username,password,nickname,phone,email,role,avatar,create_time,update_time) " +
            " VALUES (#{username},#{password},#{nickname},#{phone},#{email},#{role},#{avatar},now(),now())")
    void insert(User user);

    @Select("SELECT * FROM base_user where username = #{username}")
    User selectByUserName(String username);

    @Select("SELECT * from base_user where id = #{id}")
    User selectById(Integer id);

    @Select("SELECT * from base_user where is_deleted = 0 and role = #{role}")
    List<User> selectAllByRole(String role);

    @Select("SELECT * from base_user where is_deleted = 0")
    List<User> selectAll();

    List<User> selectByCondition(User user);

    @Update("UPDATE base_user set nickname = #{nickname}, phone = #{phone}, email = #{email}, avatar = #{avatar}," +
            "role = #{role}, update_time = now() where id = #{id}")
    void updateById(User user);

    @Update("UPDATE base_user set avatar = #{avatarUrl}, update_time = now() where id = #{id}")
    void updateAvatar(String avatarUrl, Integer id);

    @Update("UPDATE base_user set password = #{newPassword}, update_time = now() where id = #{id}")
    void updatePassword(String newPassword, Integer id);

    @Update("update base_user set is_deleted = 1, deleted_time = now(), deleted_id = #{operatorId} where id = #{deleteId}")
    void deleteById(Integer deleteId, Integer operatorId);


}
